package com.putao.album.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.main.bean.AlbumListItem;
import com.putao.album.util.BitmapHelper;
import com.putao.album.util.DisplayHelper;

import java.util.ArrayList;

/**
 * Created by yanglun on 15/6/5.
 */
public class MultiPhotoListItemView extends ViewGroup {

    private Context mContext;
    private int mType;
    public ArrayList<AlbumListItem.AlbunPicItem> mAlbumPicItem;
    int mCellPadding;
    int mCellWidth;
    int mCellHeight;
    DisplayImageOptions options;

    public MultiPhotoListItemView(Context context) {
        super(context, null);
    }

    public MultiPhotoListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        mCellPadding = DisplayHelper.dipTopx(2);
        options = new DisplayImageOptions.Builder().showImageOnLoading(BitmapHelper.getLoadingDrawable())
                .considerExifParams(true)
                .showImageOnFail(BitmapHelper.getLoadingDrawable()).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    public void setAlbumPicItem(ArrayList<AlbumListItem.AlbunPicItem> aAlbumPicItem) {
        this.mAlbumPicItem = aAlbumPicItem;
        this.removeAllViews();
        int type = getItemViewType();
        for (int i = 0; i < type; i++) {
            ImageView imgView = new ImageView(mContext);
            this.addView(imgView);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int heightSize = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);


        switch (getItemViewType()) {
            case 1: {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.AT_MOST);
            }
            break;
            case 2: {
                mCellWidth = (widthSize - mCellPadding) / 2;
                mCellHeight = mCellWidth;
                int height = mCellHeight;
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
            }
            break;
            case 3: {
                mCellWidth = (widthSize - (2 * mCellPadding)) / 3;
                mCellHeight = mCellWidth;
                int height = mCellHeight * 2 + mCellPadding;
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
            }
            break;
            case 6: {
                mCellWidth = (widthSize - (2 * mCellPadding)) / 3;
                mCellHeight = mCellWidth;
                int height = mCellHeight * 2 + mCellPadding;
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
            }
            break;
        }
//        measureChildren(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r - l;
        int height = b - t;
        if (getItemViewType() == 1) {
            ImageView view = (ImageView) this.getChildAt(0);
            view.layout(0, 0, width, height);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageLoader.getInstance().displayImage(mAlbumPicItem.get(0).picUrl, view, options);
        } else if (getItemViewType() == 2) {
            int x = 0;
            int y = 0;
            for (int i = 0; i < 2; i++) {
                ImageView view = (ImageView) this.getChildAt(i);
                view.layout(x, y, x + mCellWidth, y + mCellHeight);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ImageLoader.getInstance().displayImage(mAlbumPicItem.get(i).picUrl, view, options);
                x += mCellWidth + mCellPadding;

            }
        } else if (getItemViewType() == 6) {
            int x = 0;
            int y = 0;
            for (int i = 0; i < 6; i++) {
                ImageView view = (ImageView) this.getChildAt(i);
                view.layout(x, y, x + mCellWidth, y + mCellHeight);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ImageLoader.getInstance().displayImage(mAlbumPicItem.get(i).picUrl, view, options);
                if ((i + 1) % 3 == 0) {
                    x = 0;
                    y += mCellHeight + mCellPadding;
                } else {
                    x += mCellWidth + mCellPadding;
                }
            }
        } else if (getItemViewType() == 3) {
            int x = 0;
            int y = 0;

            ImageView view1 = (ImageView) this.getChildAt(0);
            view1.layout(x, y, (x + mCellWidth + mCellWidth + mCellPadding), (y + mCellHeight + mCellHeight + mCellPadding));
            view1.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageLoader.getInstance().displayImage(mAlbumPicItem.get(0).picUrl, view1, options);

            x = mCellWidth + mCellWidth + mCellPadding + mCellPadding;
            ImageView view2 = (ImageView) this.getChildAt(1);
            view2.layout(x, y, x + mCellWidth, y + mCellHeight);
            view2.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageLoader.getInstance().displayImage(mAlbumPicItem.get(1).picUrl, view2, options);

            y = mCellHeight + mCellPadding;
            ImageView view3 = (ImageView) this.getChildAt(2);
            view3.layout(x, y, x + mCellWidth, y + mCellHeight);
            view3.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageLoader.getInstance().displayImage(mAlbumPicItem.get(2).picUrl, view3, options);

        }
    }


    public int getItemViewType() {
        int size = mAlbumPicItem.size();
        if (size == 1) {
            return 1;
        } else if (size == 2) {
            return 2;
        } else if (size >= 3 && size < 6) {
            return 3;
        } else {
            return 6;
        }
    }
}
